﻿using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using ESRI.ArcGIS.Display;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using PlanningGIS.ArcGIS;
using PlanningGIS.Util;

namespace PlanningGIS.Symbol
{
    /// <summary>
    /// 
    /// </summary>
    internal class StyleCreator
    {
        /// <summary>
        /// 
        /// </summary>
        public StyleCreator()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        private const string _Extension = "*.style";

        /// <summary>
        /// 
        /// </summary>
        public Command StyleCommand { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ItemPanel StylePanel { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public void AddFiles(ComboBoxEx cmbStyles)
        {
            if (string.IsNullOrWhiteSpace(AoHelper.StylePath) || !Directory.Exists(AoHelper.StylePath)) return;
            try
            {
                string[] files = Directory.GetFiles(AoHelper.StylePath, _Extension);
                if (files == null || files.Length == 0) return;
                List<ValueName> fis = new List<ValueName>();
                fis.Add(new ValueName() { Name = "全部", Value = "" });
                foreach (string s in files)
                {
                    fis.Add(new ValueName() { Name = Path.GetFileName(s), Value = s });
                }
                cmbStyles.DisplayMember = "Name";
                cmbStyles.ValueMember = "Value";
                cmbStyles.DataSource = fis;
                cmbStyles.SelectedIndex = 0;
            }
            catch
            {
               
            }


        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="styletype"></param>
        /// <param name="filename"></param>
        public List<string> CreateStyles(zjgisStyleGalleryType styletype, string filename)
        {            
            string[] files = null;
            if (string.IsNullOrWhiteSpace(filename))
            {
                if (string.IsNullOrWhiteSpace(AoHelper.StylePath) || !Directory.Exists(AoHelper.StylePath)) return null;
                files = Directory.GetFiles(AoHelper.StylePath, _Extension);
            }
            else
            {
                files = (new List<string>() { filename }).ToArray();
            }
            if (files == null || files.Length == 0) return null;
            List<string> categorys = new List<string>();
            StylePanel.Items.Clear();
            GC.Collect();
            StylePanel.RecalcLayout();
            StylePanel.SuspendLayout();
            StylePanel.BeginUpdate();
            foreach (string s in files)
            {
                try
                {
                    List<SymbolItem> syms = SymbolHelper.GetStyles(styletype, s);
                    if (syms == null || syms.Count == 0) continue;
                    var grpsym = syms.GroupBy(t => t.Category).ToList();
                    string fname = Path.GetFileNameWithoutExtension(s);
                    if ((syms.Count - grpsym.Count) < syms.Count / 2)
                    {
                        ItemContainer pnl = GetCategoryPanel(string.Format("{0}.{1}", fname, "Default"));
                        categorys.Add(pnl.Name);
                        AddItems(syms, pnl);
                    }
                    else
                    {
                        foreach (var g in grpsym)
                        {
                            ItemContainer pnl = GetCategoryPanel(string.Format("{0}.{1}", fname, g.Key));
                            categorys.Add(pnl.Name);
                            AddItems(g, pnl);
                        }
                    }
                }
                catch
                {
                 
                }


            }
            StylePanel.EndUpdate();
            StylePanel.Refresh();
            return categorys;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="symbols"></param>
        /// <param name="pnl"></param>
        private void AddItems(dynamic symbols, ItemContainer pnl)
        {
            foreach (dynamic itm in symbols)
            {
                try
                {
                    ButtonItem btn = new ButtonItem();
                    btn.FixedSize = new Size(70, 80);
                    btn.OptionGroup = "Symbol";
                    btn.ColorTable = eButtonColor.Flat;
                    btn.ImagePosition = eImagePosition.Top;
                    btn.Text = itm.Name;
                    btn.Tag = itm.Symbol;
                    btn.Command = StyleCommand;
                    btn.Image = SymbolHelper.DrawSymbol2(itm.Symbol as ISymbol, 64, 64);
                    pnl.SubItems.Add(btn);
                }
                catch
                {
                   
                }

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="itmpnlStyles"></param>
        /// <returns></returns>
        private ItemContainer GetCategoryPanel(string name)
        {
            ItemContainer pnl = new ItemContainer();
            pnl.BackgroundStyle.CornerType = eCornerType.Square;
            pnl.MultiLine = true;
            pnl.Name = name;
            pnl.TitleStyle.BackColor = Color.FromArgb(((int)(((byte)(198)))), ((int)(((byte)(195)))), ((int)(((byte)(255)))));
            pnl.TitleStyle.BackColor2 = Color.FromArgb(((int)(((byte)(198)))), ((int)(((byte)(195)))), ((int)(((byte)(0)))));
            pnl.TitleStyle.BackColorGradientType = eGradientType.Radial;
            pnl.TitleStyle.BorderBottom = eStyleBorderType.Solid;
            pnl.TitleStyle.BorderBottomWidth = 1;
            pnl.TitleStyle.BorderLeft = eStyleBorderType.Solid;
            pnl.TitleStyle.BorderLeftWidth = 1;
            pnl.TitleStyle.BorderRight = eStyleBorderType.Solid;
            pnl.TitleStyle.BorderRightWidth = 1;
            pnl.TitleStyle.BorderTop = eStyleBorderType.Solid;
            pnl.TitleStyle.BorderTopWidth = 1;
            pnl.TitleStyle.CornerType = eCornerType.Square;
            pnl.TitleStyle.Font = new Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            pnl.TitleStyle.TextColor = Color.FromArgb(((int)(((byte)(66)))), ((int)(((byte)(0)))), ((int)(((byte)(66)))));
            pnl.TitleText = name;
            StylePanel.Items.Add(pnl);
            return pnl;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="symbol"></param>
        public void SetSelected(ISymbol symbol)
        {
            //try
            //{
            //    ILineSymbol lsym = (ILineSymbol)SelectedSymbol;
            //    clrLine.SelectedColor = Convertor.IColor2Color(lsym.Color);
            //    iptLWidth.Value = lsym.Width;
            //}
            //catch
            //{

            //}
        }
    }
}
